package com.example.demo.controller;

//要返回页面, 不加@ResponseBody

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@Controller
public class TestController {

    //forward方法一:
    @RequestMapping("/fw")
    public String myForward(){
        return "forward:/login.html";// forward也可以省略,因为返回页面本身就是一个请求转发
    }

    //forward方法二:
    @RequestMapping("/fw2")
    public String myForward2(
            HttpServletRequest request, HttpServletResponse response){
        try {
            request.getRequestDispatcher("/calc.html").forward(request,response);
        } catch (ServletException | IOException e) {
            e.printStackTrace();
        }
        return "forward:/login.html";
    }

    //redirect方法一:
    @RequestMapping("/rd")
    public String myRedirect(){
        return "redirect:/calc.html";
    }

    //redirect方法二:
    @RequestMapping("/rd2")
    public void myRedirect2(HttpServletResponse response){
        try {
            response.sendRedirect("/calc.html");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
